package com.eh.ucentermember.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eh.commonutils.JwtUtils;
import com.eh.commonutils.MD5;
import com.eh.servicebase.exceptionhandler.TaxException;
import com.eh.ucentermember.entity.Member;
import com.eh.ucentermember.entity.vo.RegisterVo;
import com.eh.ucentermember.mapper.MemberMapper;
import com.eh.ucentermember.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author erhuo
 * @since 2021-01-22
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

    @Override
    public String login(Member member) {
        // 获取手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        // 手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) throw new TaxException(20001, "用户名或手机号不存在，登陆失败...");

        // 判断手机号是否正确
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Member mobileMember = baseMapper.selectOne(wrapper);
        // 判断查询对象是否为空
        if (mobileMember == null) throw new TaxException(20001, "手机号不正确，登录失败...");

        // 对密码进行MD5加密
        String encryptPassword = MD5.encrypt(password);
        // 判断密码
        if (!encryptPassword.equals(mobileMember.getPassword())) throw new TaxException(20001, "密码错误，登录失败...");

        // 判断用户是否并禁用
        if (mobileMember.getIsDisabled()) throw new TaxException(20001, "用户已被禁用，登录失败...");

        // 登陆成功
        // 根据ID和昵称生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册数据
        String code = registerVo.getCode();// 验证码
        String nickname = registerVo.getNickname();// 昵称
        String mobile = registerVo.getMobile();// 手机号
        String password = registerVo.getPassword();// 密码
        // 非空判断
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new TaxException(20001, "选项不能为空，注册失败...");
        }
        // 判断验证码是否正确
        // 获取redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!redisCode.equals(code)) throw new TaxException(20001, "验证码错误，注册失败...");

        // 判断手机号是否存在（重复）
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count != 0) throw new TaxException(20001, "手机号已被注册，注册失败...");

        // 最后开始添加用户
        Member ucenterMember = new Member();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false); // 用户是否禁用，表里默认为false
        ucenterMember.setAvatar("https://erhuoweirdo-edu.oss-cn-beijing.aliyuncs.com/2021/01/03/35b804c0-9736-4c9a-a74a-d6d9e939780af82715477b0ce519cfa3c9d7fc64eda1.jpg"); // 设置默认头像
        baseMapper.insert(ucenterMember);
    }
}
